package com.service.database.execute;

import cn.hutool.core.util.ArrayUtil;
import com.service.database.model.DataColumn;
import com.service.database.action.SqlAdapter;
import com.service.database.config.DatabaseConfig;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 查询对象
 * @author xianggj
 * @Date 2021/6/4 18:47
 **/
public abstract class Query {

    /**
     * 数据库配置信息
     */
    private DatabaseConfig configuration;

    /**
     * 执行主sql语句
     */
    private String sql;

    /**
     * 执行附件sql
     */
    private List<String> batchSql;

    /**
     * 防sql注入
     */
    private Object[] paramers;

    /**
     * 设计不合理 但是没办法了
     */
    private List<DataColumn> cols = new ArrayList<>();
    /**
     * 设计不合理 但是没办法了
     */
    private List<Map<String,Object>> rs = new ArrayList<>();

    /**
     * 查询条数
     */
    private Integer num;

    /**
     * 分页查询
     */
    private Integer offset;

    private Query() {
    }

    public Query(DatabaseConfig configuration, String sql) {
        this.configuration = configuration;
        this.sql = sql;
    }

    public Query(DatabaseConfig configuration, String sql, Integer num) {
        this.configuration = configuration;
        this.num = num;
        if (num != null){
            SqlAdapter sqlAdapter = new SqlAdapter().setDataBase(configuration.getType());
            sql = sqlAdapter.addLimitBySql(sql, num, offset);
        }
        this.sql = sql;
    }

    public Query(DatabaseConfig configuration, String sql, Integer num, Integer offset) {
        this.configuration = configuration;
        this.num = num;
        this.offset = offset;
        if (num != null){
            SqlAdapter sqlAdapter = new SqlAdapter().setDataBase(configuration.getType());
            sql = sqlAdapter.addLimitBySql(sql, num, offset);
        }
        this.sql = sql;
    }

    public List<Map<String, Object>> getRs() {
        return rs;
    }

    public void setRs(List<Map<String, Object>> rs) {
        this.rs = rs;
    }

    public Object[] getParamers() {
        return paramers;
    }

    public void addParamers(Object param){
        this.paramers = ArrayUtil.append(paramers, param);
    }

    public List<DataColumn> getCols() {
        return cols;
    }

    public void setCols(List<DataColumn> cols) {
        this.cols = cols;
    }
    public void addCols(DataColumn cols) {
        this.cols.add(cols);
    }

    /**
     * 此处需要用户自己实现数据解析逻辑
     * @return
     * @throws Exception
     */
    public abstract Object doProcess() throws Exception ;

    public DatabaseConfig getConfiguration() {
        return configuration;
    }

    public void setConfiguration(DatabaseConfig configuration) {
        this.configuration = configuration;
    }

    public String getSql() {
        return sql;
    }

    public void setSql(String sql) {
        this.sql = sql;
    }

    public List<String> getBatchSql() {
        if (batchSql == null){
            return new ArrayList<>();
        }
        return batchSql;
    }

    public void setBatchSql(List<String> batchSql) {
        this.batchSql = batchSql;
    }
}
